package com.zeng.sim.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zeng.sim.controller.domain.SimMsgRecord;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.vo.DictDataItem;
import com.zeng.sim.mapper.model.SimMsgRecordEntity;
import com.zeng.sim.service.SimMsgRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-22
 */
@Controller
@RequestMapping("/sim/msg")
public class SimMsgRecordController {

    @Autowired
    private SimMsgRecordService simMsgRecordService;

    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public Result remove(@PathVariable Integer id) {
        return Result.decide(simMsgRecordService.removeById(id));
    }

    @DeleteMapping("/batchRemove/{ids}")
    @ResponseBody
    public Result batchRemove(@PathVariable String ids) {
        List<Integer> idList = ListUtil.toList(ids.split(","))
            .stream()
            .mapToInt(Integer::parseInt)
            .boxed()
            .collect(Collectors.toList());
        return Result.decide(simMsgRecordService.removeBatchByIds(idList));
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody SimMsgRecord simMsgRecord) {
        LambdaQueryWrapper<SimMsgRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SimMsgRecordEntity::getBusiType, simMsgRecord.getBusiType());
        wrapper.eq(SimMsgRecordEntity::getChannelNo, simMsgRecord.getChannelNo());
        wrapper.eq(SimMsgRecordEntity::getMsgCode, simMsgRecord.getMsgCode());
        long count = simMsgRecordService.count(wrapper);
        if (count > 0) {
            return Result
                .failure("当前渠道号[" + simMsgRecord.getChannelNo() + "]，报文编码[" + simMsgRecord.getMsgCode() + "]已存在");
        }
        SimMsgRecordEntity simMsgRecordEntity = new SimMsgRecordEntity();
        BeanUtils.copyProperties(simMsgRecord, simMsgRecordEntity);
        simMsgRecordEntity.setMsgContent(HtmlUtils.htmlUnescape(simMsgRecord.getMsgContent()));
        return Result.decide(simMsgRecordService.save(simMsgRecordEntity));
    }

    @PutMapping("/update")
    @ResponseBody
    public Result update(@RequestBody SimMsgRecord simMsgRecord) {
        SimMsgRecordEntity simMsgRecordEntity = new SimMsgRecordEntity();
        BeanUtils.copyProperties(simMsgRecord, simMsgRecordEntity);
        simMsgRecordEntity.setMsgContent(HtmlUtils.htmlUnescape(simMsgRecord.getMsgContent()));
        return Result.decide(simMsgRecordService.updateById(simMsgRecordEntity));
    }

    @GetMapping("/server/msgCode/list")
    @ResponseBody
    public Result<List<DictDataItem>> serverMsgCodeList() {
        return Result.success(simMsgRecordService.serverMsgCodeList());
    }

}
